//
//  BasePageCollectionViewController.swift
//  CreateChina
//
//  Created by 贾培军 on 2020/4/18.
//  Copyright © 2020 贾培军. All rights reserved.
//

import UIKit

open class SWBasePageCollectionViewController: SWBasePageViewController,UICollectionViewDelegate, UICollectionViewDataSource {
    open func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return dataArray.count
    }
    
    open func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        return UICollectionViewCell()
    }
    
    public let identifier = "cell"
    public var layout = UICollectionViewFlowLayout()
    public lazy var collectionView = UICollectionView.init(frame: .zero, collectionViewLayout: self.layout)
    public var plain = false
    
    open func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return dataArray.count
    }
    
    open func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        return UITableViewCell()
    }
    
    open func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
        return UIView.init();
    }
    open func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
        return UIView.init();
    }
    open func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        return 0.1;
    }
    open func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
        return 0.1;
    }
    
    open override func checkHasMoreWithCount(total: NSInteger) {
        collectionView.es.stopLoadingMore()
        if total < dataFetcher!.pageNum {
            collectionView.es.noticeNoMoreData()
        }
    }
    
    open override func endHeader(){
        collectionView.es.stopPullToRefresh()
    }
    
    open override func endFooter() {
        collectionView.es.stopLoadingMore()
    }
    
    open override func viewDidLoad() {
        super.viewDidLoad()
        collectionView.delegate = self
        collectionView.dataSource = self
        collectionView.backgroundColor = .white
        //        tableView.contentInsetAdjustmentBehavior = .never
        contentView.addSubview(collectionView)
        collectionView.snp.makeConstraints({ (m) in
            m.edges.equalTo(0)
        })
        collectionView.es.addPullToRefresh(animator: pullHeader) {
            [unowned self] in
            self.firstPageFetch()
        }
        collectionView.es.addInfiniteScrolling(animator: pullFooter) {
            [unowned self] in
            self.morePageFetch()
        }
        // Do any additional setup after loading the view.
    }
    
    open override func reloadAndReset() {
        collectionView.reloadData()
//        collectionView.contentOffset = .zero
        
    }
    
    open override func reload() {
        collectionView.reloadData()
    }
    
    
    /*
     // MARK: - Navigation
     
     // In a storyboard-based application, you will often want to do a little preparation before navigation
     override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
     // Get the new view controller using segue.destination.
     // Pass the selected object to the new view controller.
     }
     */
    
}
